﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


namespace KunURP.Editor
{
    public class KunKeywordEnumDrawer : KunSubDrawer
    {
        private string[] m_Keywords;
        private int[] m_Indexes;

        public KunKeywordEnumDrawer(string group, string w1)
        : this(group, new string[]{w1}) {}
        public KunKeywordEnumDrawer(string group, string w1, string w2)
        : this(group, new string[]{w1, w2}) {}
        public KunKeywordEnumDrawer(string group, string w1, string w2, string w3)
        : this(group, new string[]{w1, w2, w3}) {}
        public KunKeywordEnumDrawer(string group, string w1, string w2, string w3, string w4)
        : this(group, new string[]{w1, w2, w3, w4}) {}
        public KunKeywordEnumDrawer(string group, string w1, string w2, string w3, string w4, string w5)
        : this(group, new string[]{w1, w2, w3, w4, w5}) {}
        public KunKeywordEnumDrawer(string group, string[] keywords) : base(group)
        {
            this.m_Keywords = keywords;
            this.m_Indexes = new int[keywords.Length];
            for (int i = 0; i < keywords.Length; i++)
            {
                keywords[i] = keywords[i].ToUpperInvariant();
                this.m_Indexes[i] = i;
                ShaderDrawUtility.RefreshGroupIsOpen(keywords[i], false);
                if (ShaderDrawUtility.HaveGroup(this.Group))
                {
                    ShaderDrawUtility.RefreshEnumKey2Group(keywords[i], this.Group);
                }
            }
        }

        public override void DrawShaderProperty(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            var rect = EditorGUILayout.GetControlRect();
            int index = (int)prop.floatValue;

            EditorGUI.BeginChangeCheck();
            EditorGUI.showMixedValue = prop.hasMixedValue;
            int num = EditorGUI.IntPopup(rect, label.text, index, this.m_Keywords, this.m_Indexes);
            EditorGUI.showMixedValue = false;
            if (EditorGUI.EndChangeCheck())
            {
                prop.floatValue = (float)num;
                if (index >= 0 && index < this.m_Keywords.Length)
                {
                    ShaderDrawUtility.RefreshMaterialsKeywordEnabled(
                        editor.targets,
                        this.m_Keywords[index],
                        false
                    );
                }
                ShaderDrawUtility.RefreshMaterialsKeywordEnabled(
                    editor.targets,
                    this.m_Keywords[num],
                    true
                );
            }
            if (index >= 0 && index < this.m_Keywords.Length)
            {
                ShaderDrawUtility.RefreshGroupIsOpen(this.m_Keywords[index], false);
            }
            ShaderDrawUtility.RefreshGroupIsOpen(this.m_Keywords[num], true);
        }
    }
}
